﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MTC.Common.DTO;
using MTC.Common.Enum;
using MTC.Web.Business;
using MTC.Web.Business.DTO;

namespace MTC.Web.Forms
{
    public partial class TaxiEdit : System.Web.UI.Page
    {
        private TaxiEntity _entity;
        Business.Taxis TaxiBO = new Business.Taxis();

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                SetupEnvironment();
                //get the plate id if available
                if (Request.Params[Params.TAXIID] != "0")
                {
                    int taxiid = Convert.ToInt32(Request.Params[Params.TAXIID]);
                    _entity = TaxiBO.GetATaxi(taxiid);
                    LoadFields();
                }
            }

        }

        private void SetupEnvironment()
        {
            cboPlate.DataSource = DropDowns.GetPlates();
            cboPlate.DataBind();

            cboVehicle.DataSource = DropDowns.GetVehicles(); 
            cboVehicle.DataBind();

        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (ValidateFields())
            {
                SetFields();
            }
        }

        private void LoadFields()
        {
            if (_entity == null)
                return;
            hfTaxiId.Value = _entity.TaxiId.ToString();
            cboPlate.SelectedItem.Value = _entity.PlateId.ToString();
            cboVehicle.SelectedItem.Value = _entity.VehicleId.ToString();
        }

        private bool SetFields()
        {
            TaxiEntity entity = new TaxiEntity();

            if (hfTaxiId.Value != "")
                entity.TaxiId = Convert.ToInt32(hfTaxiId.Value);

            entity.VehicleId = Convert.ToInt32(cboVehicle.SelectedItem.Value);
            entity.PlateId = Convert.ToInt32(cboPlate.SelectedItem.Value);

            if (entity.TaxiId == 0)
            {
                TaxiBO.AddTaxi(entity);
            }
            else
            {
                return TaxiBO.UpdateATaxi(entity);
            }
            return true;

        }

        private bool ValidateFields()
        {
            return true;
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            ClearFields();
            Response.Redirect(Common.Urls.TaxiListURL(), true);

        }

        private void ClearFields()
        {
            cboVehicle.ClearSelection();
            cboPlate.ClearSelection();
        }

    }

    }
